package com.zy.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.bean.TAdmin;
import com.zy.bean.TAdminRole;
import com.zy.bean.TPermission;
import com.zy.bean.TRole;
import com.zy.service.TAdminRoleService;
import com.zy.service.TAdminService;

import com.zy.service.TPermissionService;
import com.zy.service.TRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.relation.Role;
import javax.servlet.http.HttpSession;
import java.security.Permission;
import java.util.*;

import static java.awt.SystemColor.window;

/**
 * @Author: zy 690748807@qq.com
 * @Param:
 * @return:
 * @Date: 2023/12/10
 * @Description: 用户表增删改查是同步数据展示（页面和 数据同时出现）
 */
@Controller
public class TAdminController {
    Logger log = LoggerFactory.getLogger(TAdminController.class);
    @Autowired
    TAdminService adminService;
    @Autowired
    TRoleService tRoleService;
    @Autowired
    TAdminRoleService tAdminRoleService;

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2024/1/13
     * @Description: 分配角色
     */

    @ResponseBody
    @RequestMapping("/admin/doAssign")
    public String doAssign(Integer[] roleId, String adminId) {
        //查询用户已有的角色集合
        List<Integer> listRoleId = tRoleService.listRoleIdByAdmin(adminId);

        //添加选中的角色
        for (Integer i : roleId) {
            listRoleId.add(i);
        }
//        List<TAdminRole> tAdminRoles = new ArrayList<>();
//        for (Integer i : listRoleId) {
//            TAdminRole tAdminRole = new TAdminRole();
//            //遍历角色集合并把角色对应用户ID存放到表
//            tAdminRole.setAdminid(Integer.valueOf(adminId));
//            tAdminRole.setRoleid(i);
//
//            tAdminRoles.add(tAdminRole);
//        }
//        for (TAdminRole tAdminRole : tAdminRoles) {
//            tAdminRoleService.instRoleIdByAdminid(tAdminRole);
//        }
        int i = tAdminRoleService.saveAdminRole(roleId, adminId);

        if (i > 0) {
            return "ok";
        }
        return "error";
    }
//
//<<<<<<< HEAD
//        /**
//         * @Author: zy 690748807@qq.com
//         * @Param:
//         * @return:
//         * @Date: 2024/1/14
//         * @Description: 取消分配角色
//         */
//        @RequestMapping("/admin/donuAssign")
//        public String donuAssign (Integer[]ids, Integer adminid){
//            return "/admin/assignRole";
//=======
//    /**
//     * @Author: zy 690748807@qq.com
//     * @Param:
//     * @return:
//     * @Date: 2024/1/14 开始
//     * @Description: 取消分配角色
//     */
//    // @RequestMapping("/admin/doUnAssign")
//    public String donuAssign(Integer[] roleId, String adminId) {
//
//        //查询用户已有的角色集合
//        List<Integer> a = tRoleService.listRoleIdByAdmin(adminId);
//
//        //更新用户对应到角色表
//        List<Integer> b = new ArrayList<>();
//        for (Integer i : roleId) {
//            b.add(i);
//>>>>>>> test
//        }
//        //1.并集
//
//        // a.addAll(b);
//
//        // 运行结果：1,2,3,2,3,3
//
//        // 2.无重复并集
////        a.removeAll(b);
////        a.addAll(b);
//
//        //  运行结果：1,2,3,3
//
//        // 3.交集
//
////        a.retainAll(b);
//
//        //   运行结果： 2,3
//
//        //  4.差集
//        TAdminRole tAdminRole = new TAdminRole();
//        int b1 = 0;
//        a.removeAll(b);
//        tAdminRoleService.delAdminid(adminId);
//        for (Integer i : a) {
//            tAdminRole.setRoleid(i);
//            tAdminRole.setAdminid(Integer.valueOf(adminId));
//            b1 = tAdminRoleService.instRoleIdByAdminid(tAdminRole);
//        }
//        if (b1 > 0) {
//            return "ok";
//        } else {
//            return "error";
//        }
//
//    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2024/1/14 开始
     * @Description: 取消分配角色
     */
    @ResponseBody
    @RequestMapping("/admin/doUnAssign")
    public String donuAssigns(Integer[] roleId, String adminId) {

        //查询用户已有的角色集合
        List<Integer> a = tRoleService.listRoleIdByAdmin(adminId);
        int i = tAdminRoleService.delAminRole(roleId, adminId);
        if (i > 0) {

            return "ok";
        } else {
            return "error";
        }

    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param: id 用户的ID
     * @return:
     * @Date: 2024/1/13
     * @Description: toAssing 跳转到权限分配页面(给某个用户)
     */
    @RequestMapping("/admin/toAssing")
    public String toAssing(String id, Model model) {

        //查询所有角色
        List<TRole> listRole = tRoleService.listRole();
        // 根据用户ID查询已经拥有的角色id
        List<Integer> listRoleId = tRoleService.listRoleIdByAdmin(id);
        // 将所有角色进行划分
        //已经分配的角色集合
        List<TRole> overfp = new ArrayList<TRole>();
        //未分配的角色集合
        List<TRole> nulloverfp = new ArrayList<TRole>();
        for (TRole tRole : listRole) {
            if (listRoleId.contains(tRole.getId())) {
//            for (Integer i : listRoleId) {
                //  if(tRole.getId().equals(i)){

                overfp.add(tRole);
            } else {

                nulloverfp.add(tRole);
            }

            model.addAttribute("overfp", overfp);
            model.addAttribute("nulloverfp", nulloverfp);
        }
        return "/admin/assignRole";
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/10
     * @Description: 批量删除
     */
    @RequestMapping("/admin/deleteBatchBtn")
    public String doDeleteAll(String ids, Integer pageNum) {
        String[] idList = ids.split(",");
        //把字符串数组转成list集合
        List<Integer> list = new ArrayList<Integer>();
        for (String s : idList) {
            Integer id = Integer.parseInt(s);
            list.add(id);
        }
        adminService.delbyIds(list);


//        return "redirect:/admin/index";
        return "redirect:/admin/index?pageNum=" + pageNum;  //添加完了回到当前一页
    }

    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名  删除
     */
    @RequestMapping("/admin/doDelete")
    public String doDelete(Integer id, Integer pageNum) {
        adminService.delbyId(id);

//        return "redirect:/admin/index";
        return "redirect:/admin/index?pageNum=" + pageNum;  //添加完了回到当前一页
    }

    /**
     * @Author: zy 690748807@qq.com
     * @Param:
     * @return:
     * @Date: 2023/12/10
     * @Description: 更新
     */
    @PostMapping("/admin/doUpdate")
    public String doUpdate(TAdmin admin, Integer pageNum) {
        adminService.updateAdmin(admin);
//        return "redirect:/admin/index";
        return "redirect:/admin/index?pageNum=" + pageNum;  //添加完了回到当前一页
    }

    @RequestMapping("/admin/toUpdate")
    public String toUpdate(Integer id, Model model) {
        TAdmin admin = adminService.selectbyId(id);
        model.addAttribute("admin", admin);
        return "admin/update";
    }

    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名  添加
     */
    @PreAuthorize("hasRole('PM - 项目经理')")
    @RequestMapping("/admin/toAdd")
    public String toAdd() {
        return "admin/add";
    }

    @RequestMapping("/admin/doAdd")
    public String doAdd(TAdmin admin) {
        Integer integer = adminService.saveAdmin(admin);
//        return "redirect:/admin/index";
        return "redirect:/admin/index?pageNum=" + Integer.MAX_VALUE;  //添加完了回到最后一页
    }

    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名 默认用户首页
     */

    @RequestMapping("/admin/index")
    public String index(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                        Model model,
                        @RequestParam(value = "condition", required = false, defaultValue = "") String condition) {

        log.debug("pageNum={}", pageNum);
        log.debug("pageSize={}", pageSize);
        log.debug("condition={}", condition);

        PageHelper.startPage(pageNum, pageSize);  //线程绑定

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("condition", condition);
        //分页查询所有
        PageInfo<TAdmin> page = adminService.listAdminPage(paramMap);
        model.addAttribute("page", page);
        //到用户维护的增删改查页面
        return "admin/index";
    }


}
